import java.awt.Point;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

import java.util.Stack;

import javax.swing.JFrame;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class CardGrid extends JPanel {
	CardGrid(Stack<Card> cardStack){
		Stack<Card> c = cardStack;
		
		int panelWidth = 0;
		int panelHeight = 0;
		
		
		setLayout(new GridLayout(HEIGHT, WIDTH));
		cards = new Card[HEIGHT][WIDTH];
		for(int y = 0; y < HEIGHT; y++){
			for(int x = 0; x < WIDTH; x++){
				cards[y][x] = c.pop();
				Dimension d = cards[y][x].getPreferredSize();
				panelWidth += d.width;
				panelHeight += d.height;
				
				add(cards[y][x]);
			}
		}

	}
 
	CardGrid(String save){
		int panelWidth = 0;
		int panelHeight = 0;
		setLayout(new GridLayout(HEIGHT, WIDTH));
		cards = new Card[HEIGHT][WIDTH];
		int y = 0;
		int x = 0;
		String s[] = save.split("\n");
		for(int i =0;i<s.length;i++){
			if(x >= WIDTH) { y++; x=0; }
			if(s[i].contains("<Card ")){
				cards[y][x] = new Card(s[i]);

				Dimension d = cards[y][x].getPreferredSize();
				panelWidth += d.width;
				panelHeight += d.height;
				add(cards[y][x]);
				x++;
			}
		}
	}
	public void flipCard(int x, int y){
		cards[y][x].flip();
	}
	
	public void addMouseListener(MouseListener m){
		for(int y = 0; y < HEIGHT; y++){
			for(int x = 0; x < WIDTH; x++){
				if(!cards[y][x].isFlipped())
				cards[y][x].addMouseListener(m);
			}
		}
	}
	public void removeMouseListener(MouseListener m){
		for(int y = 0; y < HEIGHT; y++){
			for(int x = 0; x < WIDTH; x++){
				cards[y][x].removeMouseListener(m);
			}
		}
	}
 
 /**
  * This does a .equals() on the two cards at the two points.
  * If they are the same return true and remove the cards. Else
  * return false and flip the cards back.
  * @param firstCard the first Card the User clicked
  * @param secondCard the second Card the User clicked
  * @return
  */
	public boolean CompareCards(Point firstCard, Point secondCard){
		if (firstCard.equals(secondCard)){return true;}
		else {return false;}
	}
 
 /**
  * @param b x, y location of the card
  * @return if you successfully flipped the card return true
  *    else if there is a complication or the card is not there return false;
  */
	public boolean flipCardAt(Point b){
		return false;
	}
	
	public int numOfCards(){
		return HEIGHT * WIDTH;
	}
	private Card[][] cards;
	
	public String toString(){
		String s = "";
		s += "<CardGrid>\n";
		for(int i = 0;i < HEIGHT; i++){
			for(int j = 0;j < WIDTH;j++){
				s += "\t"+cards[i][j]+"\n";
			}
		}
		s += "</CardGrid>";
		return s;
	}
	
	private static final int HEIGHT = 3;
	private static final int WIDTH = 4;
	
	/* Test Main
	public static void main(String[] args){
		JFrame f = new JFrame();
		f.add(new CardGrid());
		f.setVisible(true);
		f.pack();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
		
	}*/
}
